package com.dragonfly.shutter.controllers;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.beans.Message;
import com.dragonfly.shutter.beans.PictureDetail;
import com.dragonfly.shutter.beans.Tag;
import com.dragonfly.shutter.services.PictureEditService;
import com.dragonfly.shutter.utils.KeywordFilter;
import com.dragonfly.shutter.utils.ListOperation;
import com.dragonfly.shutter.validator.PictureEditValidator;

/**
 * 图片编辑控制类
 * @author 黄挺
 */
@Controller
@RequestMapping("/pictureEdit.html")
public class PictureEditController {
	@Autowired
	private PictureEditService pictureEditService;		//图片编辑服务类
	private List<Tag> pictureTags;						//图片标签，用于保存图片标签
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView onSubmit(@ModelAttribute("picture") PictureDetail picture,
			BindingResult result, HttpServletRequest request, ModelMap map){
		String requestUrl = request.getRequestURL().toString();
		String tagString = request.getParameter("pictureTags");
		picture.setPictureTags(getPictureTags());
		
		//验证标签的合法性
		List<String> tags = ListOperation.stringToList(tagString, "\\s{1,}");
		
		if(ListOperation.filtList(tags)){
			map.addAttribute("tagError", true);
			return new ModelAndView("pictureEdit");
		}
		//验证提交信息的合法性
		int oldErrorCount = result.getErrorCount();
		new PictureEditValidator().validate(picture, result);
		int newErrorCount = result.getErrorCount();
		
		if( oldErrorCount != newErrorCount ){
			return new ModelAndView("pictureEdit");
		}
		pictureEditService.doService(picture, tags, requestUrl);
		
		return new ModelAndView("redirect:/picture.html?pictureID="+picture.getPictureID());
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView onLoad(HttpSession session, ModelMap model,
			@RequestParam(value="pictureID", required=false) String pictureID,
			@RequestParam(value="ownerID", required=false) String ownerIDStr){
		Integer userID = (Integer)session.getAttribute("userID");
		Message message = null;
		
		//不知道编辑什么图片
		if ( pictureID == null || ownerIDStr == null){
			message = new Message("您所要编辑的图片不存在", "图片找不到", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		//参数安全验证
		if( !KeywordFilter.isNumeric(pictureID) || !KeywordFilter.isNumeric(ownerIDStr)){
			message = new Message("非法的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		Integer ownerID = Integer.parseInt(ownerIDStr);
		
		//用户权限不够
		if ( userID == null || ownerID == null || ( userID != ownerID && ownerID != 0 ) ) {
			message = new Message("您不是这样图片的所有者，无法编辑这张图片", "权限不够", "/Dragonfly/picture.html?pictureID=" + pictureID);
			return new ModelAndView("message", "message", message);
		}
		
		PictureDetail picture = pictureEditService.getPictureDAO().selectPicByPicID(pictureID);
		setPictureTags(picture.getPictureTags());
		model.addAttribute("picture",picture);
		
		return new ModelAndView("pictureEdit","picture", picture);
	}

	public void setPictureTags(List<Tag> pictureTags) {
		this.pictureTags = pictureTags;
	}

	public List<Tag> getPictureTags() {
		return pictureTags;
	}
}
